package com.example.webpractice.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.webpractice.dao.XueshengDao;
import com.example.webpractice.dto.XueshengDTO;
import com.example.webpractice.po.XueshengPO;
import com.example.webpractice.vo.PageVo;
import com.example.webpractice.vo.Po2Vo;
import com.example.webpractice.vo.XueshengVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("xueshengService")
public class XueshengService {


    @Autowired
    private XueshengDao dao;

    public XueshengPO selectByUsername(String username) {
        return dao.selectOne(new QueryWrapper<XueshengPO>().eq("xuehao", username));
    }

    public XueshengVO selectByXuehao(String xuehao) {
        XueshengPO po = dao.selectOne(new QueryWrapper<XueshengPO>().eq("xuehao", xuehao));
        if (po != null) {
            XueshengVO vo = new XueshengVO();
            BeanUtils.copyProperties(po, vo);
            return vo;
        }
        return null;
    }

    public void create(XueshengDTO xueshengDTO) {
        XueshengPO po = new XueshengPO();
        BeanUtils.copyProperties(xueshengDTO, po);
        dao.insert(po);
    }

    public int updateXuesheng(XueshengDTO xueshengDTO) {
        XueshengPO po = new XueshengPO();
        BeanUtils.copyProperties(xueshengDTO, po);
        return dao.updateById(po);
    }

    public XueshengVO selectById(Long id) {
        XueshengPO po = dao.selectOne(new QueryWrapper<XueshengPO>().eq("id", id));
        if (po != null) {
            XueshengVO vo = new XueshengVO();
            BeanUtils.copyProperties(po, vo);
            return vo;
        }
        return null;
    }

    public PageVo queryPage(int pages, int limit, QueryWrapper queryWrapper) {
        Page<XueshengPO> pagePO = dao.selectPage(new Page<>(pages, limit), queryWrapper);
        return Po2Vo.toVoPage(pagePO, XueshengVO.class);
    }

    public int updateById(XueshengPO xueshengPO) {
        return dao.updateById(xueshengPO);
    }


    public int deleteBatchIds(List list) {
        return dao.deleteByIds(list);
    }

}
